// 分配计划编辑控制器
define('salesPersonController', ['angular','jquery'], function (angular,$) {
    var module = angular.module('controllers');
    module.controller('salesPersonController', ['$scope','$filter','$route','$routeParams','userServers','salesServers',function ($scope,$filter,$route,$routeParams,userServers,salesServers) {
        $scope.companyPlan=[];
        $scope.currentData=[];
        $scope.companyId=$routeParams.branch_company_id;
        $scope.month=$routeParams.month;
        $scope.currentCompanyInfo={
            branch_company_id:$scope.companyId,
            month:$scope.month,
            type:2
        };
        salesServers.searchSales($scope.currentCompanyInfo).then(function(data){
            $scope.companyPlan=data[0];
        });
        // 添加个人计划
        $scope.addSalesData={};
        $scope.addSalesData.user_id='';
        $scope.addSalesData.branch_company_id=$scope.companyId;
        $scope.addSalesNameForm=function(valid){
            if(valid){
                $scope.addSalesData = $filter('simplifyJsonFilter')($scope.addSalesData);
                salesServers.addSales($scope.addSalesData).then(function (data) {
                    if(data){
                        $route.reload();
                    }else{
                        alert("添加失败了么");
                    }
                });
            }else{
                alert("请填写全部信息");
            }
        };
        //搜索
        $scope.setData={};
        $scope.table_list=[];
        $scope.formData={
            user_id:"",
            type:2
        };
        $scope.searchSalesForm=function(){
            $scope.formData = $filter('simplifyJsonFilter')($scope.formData);
            // console.log($scope.formData);
            salesServers.searchSales($scope.formData).then(function(data) {
                $scope.table_list=data;
                userServers.userList().then(function (data) {
                    $scope.userList=data;
                    for(var i=0;i<$scope.table_list.length;i++){
                        for(var j=0;j<$scope.userList.length;j++){
                            if($scope.table_list[i].user_id==$scope.userList[j].id){
                                $scope.table_list[i].user_name=angular.copy($scope.userList[j].name);
                            }
                        }
                    }                    
                });
            });
        };
        $scope.searchSalesForm();
        $scope.$watch("currentData",function(n,o){
            $scope.sum();
        });
        $scope.sum=function(){
            $scope.sumData={
                guarantee_planning:0,
                task_planning:0,
                target_planning:0,
                reality_money:0
            };
            for (var i = 0; i < $scope.currentData.length; i++) {
                  $scope.sumData.guarantee_planning+=$scope.currentData[i].guarantee_planning;
                  $scope.sumData.task_planning+=$scope.currentData[i].task_planning;
                  $scope.sumData.target_planning+=$scope.currentData[i].target_planning;
                  $scope.sumData.reality_money+=$scope.currentData[i].reality_money;
            }
        };
        // 显示模态框，给框里设置当前选中的值
        $scope.setplan=function(v){
            $scope.setSalesData=angular.copy(v);
        };
        //修改
        $scope.setForm=function(){
            $scope.setSalesData.branch_company="";
            delete $scope.setSalesData.branch_company;
            $scope.setSalesData = $filter('simplifyJsonFilter')($scope.setSalesData);
            salesServers.setplan($scope.setSalesData).then(function(data){
                if(data.data===true){
                    $route.reload();
                    $("#myModal").modal('hide');
                }else{
                    alert('失败');
                }
            });
        };
        //删除
        $scope.delplan=function(v){
            if(confirm('确定删除?')===false)
                return false;
            salesServers.delplan(v).then(function(data) {
                if(data.data===true){
                    $route.reload();
                }else{
                    alert('失败');
                }
            });                 
        }; 
        //全选，删除 
        $scope.checked=[];
            $scope.allSelect=function(){
                $scope.checked.forEach(function(v,i){
                    $scope.checked[i]=!v;
            });
        };
        $scope.$watch('checked',function(n,o){
                $scope.selectedData=[];
                var tempObj={};
                $scope.checked.forEach(function(v,i){
                    tempObj={};
                    if(v){
                        tempObj=$scope.table_list[i];
                        tempObj=$scope.table_list[i];
                        $scope.selectedData.push(tempObj);
                    }
                });
            },true);
        $scope.delSubmit=function(){
            if(confirm('确定删除?')===false)
                return false;                                   
            salesServers.delplan($scope.selectedData).then(function(data){
                if(data.data){
                    $route.reload();
                }else{
                    alert("删除失败");
                }
            });
        };
    }]);
});